package ar.posti.framework.query.hibernate;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.ScrollableResults;

import ar.posti.framework.query.core.QueryExecutor;

public class HibernateQueryExecutor implements QueryExecutor<Query>{

	public int queryForInt(Query query) {
		return ((Integer) this.queryForObject(query)).intValue();
	}

	public List queryForList(Query query) {
		return query.list();
	}

	public Object queryForObject(Query query) {
		return query.uniqueResult();
	}

	public Iterator queryForIterator(Query query) {
		return ScrollableResultsIterator(query.scroll());
	}
	
	private Iterator ScrollableResultsIterator(final ScrollableResults scroll) {
		return new Iterator() {
			public boolean hasNext() {
				return !scroll.isLast();
			}

			public Object next() {
				return scroll.next();
			}

			public void remove() {
				throw new UnsupportedOperationException();
			} 
		};
	}
}
